package com.zyh.supermark.system.dao.impl;

import com.zyh.supermark.system.dao.UserDao;
import com.zyh.supermark.system.domain.User;

import java.util.ArrayList;

public class UserDaoImpl implements UserDao {
    //创建集合
    public static final ArrayList<User> USER_LIST = new ArrayList<User>();

    //初始化数据
    static {
        USER_LIST.add(new User(1001, "冯程程", "铂金卡", 2000, "合法账户"));
        USER_LIST.add(new User(1003, "孙悟空", "钻石卡", 5000, "被禁账户"));
        USER_LIST.add(new User(1002, "张巧巧", "黄金卡", 1000, "合法账户"));
    }

    @Override
    public void findAll() {
        //打印表头
        System.out.println("用户ID\t账号\t\t卡类型\t积分\t\t卡片状态");
        for (User user : USER_LIST) {
            System.out.println(user.getUserId() + "\t" + user.getUserName() + "\t" + user.getCardType() + "\t" + user.getScore() + "\t" + user.getCardState());
        }
    }

    @Override
    public boolean delete(int id) {
        int deleteId = findById(id);
        if (deleteId == -1) {
            return false;
        }
        USER_LIST.remove(deleteId);
        return true;
    }

    @Override
    public boolean add(User user) {
        USER_LIST.add(user);
        return true;
    }

    @Override
    public int findById(int id) {
        //假设要查询的id不在索引中
        int index = -1;
        for (int i = 0; i < USER_LIST.size(); i++) {
            User user = USER_LIST.get(i);
            if (id == user.getUserId()) {
                index = i;
            }
        }
        return index;
    }
}
